<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件
/**
 * $msg 待提示的消息
 * $url 待跳转的链接
 * $icon 这里主要有两个，5和6，代表两种表情（哭和笑）
 * $time 弹出维持时间（单位秒）
 */
function jumpinfo($message, $url, $jptime = 1,$icon = 6) {
    $str="<style>#layui-layer1{ width: 240px !important;}</style>";
    $str.= "<script src=\"" .  config('WEB_URL') . "static/hui/lib/jquery/1.9.1/jquery.min.js\" type=\"text/javascript\"></script>";
    $str.=  "<script src=\"" . config('WEB_URL') . "static/hui/lib/layer/2.4/layer.js\" type=\"text/javascript\"></script>";
    $str.=  "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0\" />";
    $str.= "<script>$(function(){layer.msg('" . $message . "',{icon:$icon,time:'" . ($jptime * 1000) . "'}, function (){location.href= '" . $url . "';})})</script>";
    return $str;
}
/**
 * 驼峰转下划线
 * @param $str
 * @return string
 */
function hump2underline($str)
{
	return strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_', $str));
}
/**
 * 下载文件
 * @param string $file
 *               被下载文件的路径
 * @param string $name
 *               用户看到的文件名
 */
function download($file,$name=''){
    $fileName = $name ? $name : pathinfo($file,PATHINFO_FILENAME);
    //$filePath = realpath($fileName);
    $filePath = $file;
    $fp = fopen($filePath,'rb');

    if(!$filePath || !$fp){
        header('HTTP/1.1 404 Not Found');
        echo "Error: 404 Not Found.(server file path error)<!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding -->";
        exit;
    }

    $fileName = $fileName .'.'. pathinfo($filePath,PATHINFO_EXTENSION);
    $encoded_filename = urlencode($fileName);
    $encoded_filename = str_replace("+", "%20", $encoded_filename);

    header('HTTP/1.1 200 OK');
    header( "Pragma: public" );
    header( "Expires: 0" );
    header("Content-type: application/octet-stream");
    header("Content-Length: ".filesize($filePath));
    header("Accept-Ranges: bytes");
    header("Accept-Length: ".filesize($filePath));

    $ua = $_SERVER["HTTP_USER_AGENT"];
    if (preg_match("/MSIE/", $ua)) {
        header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
    } else if (preg_match("/Firefox/", $ua)) {
        header('Content-Disposition: attachment; filename*="utf8\'\'' . $fileName . '"');
    } else {
        header('Content-Disposition: attachment; filename="' . $fileName . '"');
    }

    // ob_end_clean(); <--有些情况可能需要调用此函数
    // 输出文件内容
    fpassthru($fp);
    exit;
}
function getcolumnname($id){
     $Columnmodel=new  \app\admin\model\Column();
     return $Columnmodel->where(['id'=>$id])->value("name");
}
function getlist(){
    $Columnmodel=new  \app\admin\model\Column();
    return $Columnmodel->where(['status'=>1])->select();
}

/**
 * Created by PhpStorm.
 * Admininfo: Administrator
 * Date: 2018/3/19 0019
 * Time: 下午 4:18
 */
/**
 * __mkdirs
 *
 * 循环建立目录的辅助函数
 *
 * @param dir    目录路径
 * @param mode    文件权限
 */
function __mkdirs($dir, $mode = 0777) {
    if (!is_dir($dir)) {
        __mkdirs(dirname($dir), $mode);
        @mkdir($dir, $mode);
        return true;
    }
}
function getusername($id){
    $Usermodel=new \app\admin\Model\User();
    return $Usermodel->where(['id'=>$id])->value("username");
}
//实现curl的post和get访问方式；
 function _request($curl, $https = true, $method = 'GET', $data = null)
{
    $ch = curl_init();    //先初始化curl函数
    curl_setopt($ch, CURLOPT_URL, $curl);
    curl_setopt($ch, CURLOPT_HEADER, false);       //不接收头部信息
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //不是直接输出
    if ($https) {     //微信的请求都是https的
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
    }
    if ($method == 'POST') {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

/**
 * @param $table
 * @param string $order
 * @param string $limit
 * @param string $relation
 * @param string $where
 * @return false|PDOStatement|string|\think\Collection
 * 获取列表数据
 */
function getlists($table,$order=" id desc", $limit="",$relation="",$where="status=1"){
    if(!$relation){
        if(!$limit){
            $list=\think\Db::name($table)->where($where)->order($order)->select();
        }else{
            $list= \think\Db::name($table)->where($where)->order($order)->limit($limit)->select();
        }
    }else{
        if(!$limit){
            $list=model($table)->relation($relation)->where($where)->select();
        }else{
            $list=model($table)->relation($relation)->limit($limit)->where($where)->select();
        }
    }
    return $list;
}
/**
 * 风格时间
 * @param int $time 开始时间,结束时间
 * @author Yoko
 */
function settime($time =array()){
//    $stimestamp = strtotime($time['begin']);
//    $etimestime = strtotime($time['end']);
    //计算日期段内有多少天
    $days = ($time['end'] - $time['begin'])/86400+1;
    //保存每天日期
    $date = array();
    /* for循环本周一到周日 */
    for($i=0; $i<$days; $i++){
        $date[] = date("Y-m-d",$time['begin']+(86400*$i));
    }
    return $date;
}


